﻿using CRMExam.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CRMExam.Database;
using Microsoft.AspNet.Identity;

namespace CRMExam.Controllers
{
    [Authorize]
    public class ContactsController : Controller
    {
        CRMExam1Entities db = new CRMExam1Entities();

        public ActionResult Show()
        {
            var currentUser = User.Identity.GetUserId();

            var model = db.Contacts.Where(c => c.AspNetUsersId == currentUser)
                .Select(x => new ShowContactViewModel
                {
                    Id = x.Id,
                    Email = x.Email,
                    MobilePhone = x.MobilePhone,
                    Name = x.Name,
                    WorkPhone = x.WorkPhone,
                    Picture = x.Picture,
                    PictureBytes = x.PictureBytes
                });
            return View(model);
        }

        public ActionResult Create()
        {
            var model = new CreateContactsViewModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CreateContactsViewModel model)
        {
            var currentUser = User.Identity.GetUserId();

            Contact contact = new Contact
            {
                Id = Guid.NewGuid(),
                AspNetUsersId = currentUser,
                Name = model.Name,
                Email = model.Email
            };

            db.Contacts.Add(contact);
            db.SaveChanges();
            return RedirectToAction("Create");
        }

        public ActionResult Edit(Guid guid)
        {
            return View();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}